package com.sinochem.es.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sinochem.es.demo.dao.RmPolicyPublicationDao;
import com.sinochem.es.demo.entity.RmPolicyPublication;
import com.sinochem.es.demo.mapper.RmPolicyPublicationMapper;
import com.sinochem.es.demo.service.IRmPolicyPublicationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.lucene.index.TieredMergePolicy;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评估资源管理政策发文信息表 服务实现类
 * </p>
 *
 * @author Barry,Yu
 * @since 2022-02-08
 */
@Service
public class RmPolicyPublicationServiceImpl extends ServiceImpl<RmPolicyPublicationMapper, RmPolicyPublication> implements IRmPolicyPublicationService {

    private static Logger logger = LoggerFactory.getLogger(RmPolicyPublicationServiceImpl.class);

    @Resource
    private RmPolicyPublicationDao rmPolicyPublicationDao;

    @Resource
    private ElasticsearchRestTemplate elasticsearchTemplate;



    @Override
    public void saveEs(List<RmPolicyPublication> list) {
        rmPolicyPublicationDao.saveAll(list);
    }

    @Override
    public List<RmPolicyPublication> highQuery() {
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("text", "化部办");


        logger.info("DLS:{}",matchQueryBuilder.toString());
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
        nativeSearchQueryBuilder.withQuery(matchQueryBuilder)
                .withFields("text")
                .withHighlightFields(new HighlightBuilder.Field("text"))
                .withHighlightBuilder(new HighlightBuilder().preTags("<span style='color:red'>").postTags("</span>"));
        NativeSearchQuery nativeSearchQuery = nativeSearchQueryBuilder.build();

        SearchHits<RmPolicyPublication> search = elasticsearchTemplate.search(nativeSearchQuery, RmPolicyPublication.class);

        List<RmPolicyPublication> list = new ArrayList<>();
        List<SearchHit<RmPolicyPublication>> searchHits = search.getSearchHits();
        for (SearchHit<RmPolicyPublication> searchHit : searchHits) {
            Map<String, List<String>> highlightFields = searchHit.getHighlightFields();
            //将高亮的内容填充到content中
            searchHit.getContent().setIssuer(highlightFields.get("text")==null ? searchHit.getContent().getIssuer():highlightFields.get("text").get(0));
            list.add(searchHit.getContent());
        }
        return list;
    }

    @Override
    public RmPolicyPublication findById(String id) {
        return rmPolicyPublicationDao.findById(id).get();
    }

    @Override
    public void saveByEs(RmPolicyPublication rmPolicyPublication) {
        rmPolicyPublicationDao.save(rmPolicyPublication);
    }


}
